//
//  MineDownloadViewController.m
//  copyrightTrading
//
//  Created by 郭子一 on 2020/10/6.
//  Copyright © 2020 郭子一. All rights reserved.
//

#import "MineDownloadViewController.h"
#import "MineDownloadCell.h"
#import "UINavigationController+FDFullscreenPopGesture.h"
@interface MineDownloadViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) NSInteger pageIndex;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation MineDownloadViewController

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.pageIndex = 1;
//    self.navigationController.fd_fullscreenPopGestureRecognizer.enabled = NO;
//    self.fd_interactivePopDisabled = YES;
}

- (void)setupUI
{
    UIView *headerView = [UIView new];
    headerView.backgroundColor = RGB_VALUE(0xDC0000);
    [self.view addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.height.mas_equalTo(Height_NavBar);
    }];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:[UIImage imageNamed:@"whiteBack"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setHitEdgeInsets:UIEdgeInsetsMake(-15, -15, -15, -15)];
    [headerView addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-14);
        make.left.offset(18);
        make.width.height.mas_equalTo(16);
    }];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"我的下载";
    titleLabel.font = JSS_MEDIUM_FONT(17);
    titleLabel.textColor = RGB_VALUE(0xFFFFFF);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [headerView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-9);
        make.centerX.offset(0);
        make.width.mas_equalTo(135);
        make.height.mas_equalTo(24);
    }];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[MineDownloadCell class] forCellReuseIdentifier:@"MineDownloadCell"];
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(headerView.mas_bottom).offset(0);
        make.bottom.offset(0);
    }];
    
    @weakify(self)
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        @strongify(self)
        self.pageIndex ++;
        [self requestData];
    }];
    [self requestData];
    
    // 获取所有缓存数据(缓存中和缓存完成的)
    self.dataArray = [[[HWDataBaseManager shareManager] getAllCacheData] mutableCopy];
    
}

- (void)backBtnClick
{
    [self pop];
}

- (void)requestData
{
    [self.tableView.mj_footer endRefreshing];
    [[APIManager sharedNetManager] requestWithMethod:HttpRequestTypeGet URLString:@"download" parameters:@{@"page":@(self.pageIndex),@"limit":@"10"} done:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MineDownloadCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineDownloadCell"];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 120;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    [self pushVideoDetailVCWithID:@""];
    HWDownloadModel *model = self.dataArray[indexPath.row];
    VideoDetailViewController *vc = [[VideoDetailViewController alloc] init];
    vc.downLoadmodel = model;
    vc.detailID = model.vid;
    [self pushVc:vc];
}

//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    HWDownloadModel *model = self.dataArray[indexPath.row];
//    [self deleteRowAtIndex:indexPath.row];
//    [[HWDownloadManager shareManager] deleteTaskAndCache:model];
//}
//
//- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    [[tableView cellForRowAtIndexPath:indexPath] setNeedsLayout];
//}
//
//- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return @"删除";
//}
//
//- (void)deleteRowAtIndex:(NSInteger)index
//{
//    [self.dataArray removeObjectAtIndex:index];
//    [_tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
//}

//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
//{
//    // 打印touch到的视图
//    NSLog(@"%@", NSStringFromClass([touch.view class]));
//   
//    // 如果视图为UITableViewCellContentView（即点击tableViewCell），则不截获Touch事件
//    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
//        return NO;
//    }
//    return  YES;
//}
@end
